package com.example.fw;

import java.util.HashSet;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;


public class ContactHelper extends HelperWithGetDriverBase {
	
	private Contacts contactModel;
	private int lastContactId;
	
	public ContactHelper(ApplicationManager manager){
        super(manager);
        lastContactId = Integer.parseInt(manager.getHibernateHelper().getLastContactId());
	}
	
	public Contacts getUIContacts() {
		HashSet<ContactObject> set = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		WebElement form = findElements(By.tagName("form")).get(2);		
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));	
		for (WebElement checkbox : checkboxes) {
			ContactObject contact = workWithStringAndCreateNewContactInstance(checkbox);
			set.add(contact);
		}
		return new Contacts(set);
	}

	public Contacts getContacts() {
		if (contactModel == null){
			contactModel = manager.getHibernateHelper().getContacts();
		}
		return contactModel;
	}
	
	private ContactObject workWithStringAndCreateNewContactInstance(
			WebElement checkbox) {
		String title = checkbox.getAttribute("title");			
		title = title.substring("Select (".length(), title.length()-")".length());
		ContactObject contact = new ContactObject()
			.setCheckboxId(checkbox.getAttribute("id"));
		return contact;
	}
	
	public void createContact(ContactObject validContact) {
		initContactCreation();
		fillContactForm(validContact, true);
		submitContactCreation();
		returnToHomePage();
		lastContactId++;
		contactModel = contactModel.withAdded(validContact);
	}

	public void modifyContact(ContactObject oldContactObject, ContactObject newContactObject) {
		initContactModification(oldContactObject);
		fillContactForm(newContactObject, false);
		submitContactModification();
		returnToHomePage();
		contactModel = contactModel.without(oldContactObject).withAdded(newContactObject);
	}
	
	public void deleteContact(ContactObject contactObject) {
		initContactModification(contactObject);
		submitContactRemove();
		returnToHomePage();
		contactModel = contactModel.without(contactObject);
	}
	
	private void fillContactForm(ContactObject contactObject, boolean fillGroup) {
		type(By.name("firstname"), contactObject.firstname );
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);		
		type(By.name("byear"), contactObject.year);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);
		dataSelected(By.name("bday"), contactObject.day);
		dataSelected(By.name("bmonth"), contactObject.month);
		if (fillGroup){
			groupSelected(By.name("new_group"), contactObject.groupName);
		}	
	}

	private void groupSelected(By locator, int groupNumberSelected){
		new Select(findElement(locator)).selectByIndex(groupNumberSelected);
	}
	
	private void dataSelected(By locator, String dataselected) {
		new Select(findElement(locator)).selectByVisibleText(dataselected);
	}

	private void returnToHomePage() {
		click(By.linkText("home page"));
	}

	private void submitContactCreation() {
		click(By.name("submit"));
	}
	
	private void initContactCreation() {
		click(By.linkText("add new"));
	}

	private void submitContactRemove() {
		click(By.xpath(".//*[@id='content']/form[2]/input[2]"));
	}	

	private void submitContactModification() {
		click(By.xpath(".//*[@id='content']/form[1]/input[11]"));
	}
	
	private void initContactModification(ContactObject contactObject) {
		checkContact(contactObject);
		click(By.xpath("/html/body/div/div[4]/form[2]/table/tbody/tr[3]/td[7]/a/img"));	
	}
		
	private void checkContact(ContactObject contactObject) {
		click(By.xpath("//input[@value='" + contactObject.checkboxId + "']"));
	}
}

	